# from utils.pipeline import Pipeline


from .logger import Logger
from .base_class import ABC
from .pipe_frame import PipeFrame
from typing import Union, Any, Sequence, List, Dict, Tuple
import cv2

import threading


class VideoPipeline(ABC):
    def __init__(self, frame: PipeFrame, fun_list: List = []):
        global static_cap
        self.frame = frame
        self.fun_list = fun_list

        # 创建一个新的线程，用于读取视频帧
        self.thread = threading.Thread(target=self.read_frames)
        self.thread.start()

    def read_frames(self):
        global static_cap
        # 打开视频文件
        static_cap = cv2.VideoCapture(self.frame.video_path)
        # 检查视频是否成功打开
        if not static_cap.isOpened():
            print("Error opening video file")

        # 读取视频帧，直到视频结束
        while static_cap.isOpened():
            # 读取一帧视频，ret 是布尔值，表示读取成功或失败
            ret, self.frame.img = static_cap.read()
            if not ret:
                break

            # `处理视频帧`
            for fun in self.fun_list:
                fun(self.frame)

            # 显示处理结果
            cv2.imshow("Video Pipeline", self.frame.img)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break

        # 释放资源
        static_cap.release()
        cv2.destroyAllWindows()

    def run(self):
        # 等待读取视频帧的线程结束
        self.thread.join()
